package require tkMD 1.1
namespace import Path::*
namespace import Data::*

##parse data from file 
# This procedure have only one arg, that is input file name.
# It will output a list in which odd number elements are the names of the collected variables, and even number elements are the values of the collected variables.
proc getS { fname } {
    set sfile [open $fname r]
    set data [read $sfile]
    set result {}
    if {[regexp -- {PEPT\_EN\_MIN\.CRD\sIN\sWATER\s:\s+(\S+)\s+CAL/MOL\*K\s+\,TS\s+\=\s+(\S+)\s+K} $data matched s ts]} {
        lappend result S,water
        lappend result $s
        lappend result TS,water
        lappend result $ts
    } else {
        lappend result S,water
        lappend result Nan
        lappend result TS,water
        lappend result Nan
    }
    if {[regexp -- {PEPT\_EN\_MIN\.CRD\sIN\sNEUTRAL\s:\s+(\S+)\s+CAL/MOL\*K\s+\,TS\s+\=\s+(\S+)\s+K} $data matched s ts]} {
        lappend result S,neutral
        lappend result $s
        lappend result TS,neutral
        lappend result $ts
    } else {
        lappend result S,neutral
        lappend result Nan
        lappend result TS,neutral
        lappend result Nan
    }
    if {[regexp -- {PEPT\_EN\_MIN\.CRD\sIN\sANIONIC\s:\s+(\S+)\s+CAL/MOL\*K\s+\,TS\s+\=\s+(\S+)\s+K} $data matched s ts]} {
        lappend result S,anionic
        lappend result $s
        lappend result TS,anionic
        lappend result $ts 
    } else {
        lappend result S,anionic
        lappend result Nan 
        lappend result TS,anionic
        lappend result Nan 
    }   
    close $sfile
    return $result
}

## Get entropy from pept.s using the procedure getS to do the parsing.
set S [collect [pwd] pept.s [pwd]/@pept/type_@type/orientation_@orientation/\\d+ getS]
puts [toTable $S]
exit


